#include "formpzcx.h"
#include "ui_formpzcx.h"
#include <QDebug>
#include <QLineEdit>
#include <QAbstractItemView>
#include <mysql.h>
#include <QScrollBar>
#include <QPalette>
#include <QDate>
#include <QInputDialog>
#include <QMenu>
#include <QMenuBar>
FormPZCX::FormPZCX(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::FormPZCX)
{
    ui->setupUi(this);
    this->setWindowTitle("凭证查询");
    RefTree();
    Start();
}
void FormPZCX::Start()
{
    QString  str;
    str = QDate::currentDate().toString("yyyy-01-01");
    ui->dtSRQ->setDate(QDate::fromString(str,"yyyy-MM-dd"));
    ui->dtERQ->setDate(QDate::currentDate());
    QString SQL="";
    QString ND=mySQL::ND;
    QSqlQuery query(mySQL::cn); //查询 表并输出，测试能否正常操作数据库
    ui->cmbJD->clear();
    ui->cmbJD->addItem("全部");
    ui->cmbJD->addItem("借方");
    ui->cmbJD->addItem("贷方");
    ui->cmbHB->clear();
    ui->cmbHB->addItem("全部");
    ui->cmbHB->addItem("CNY");
    ui->cmbHB->addItem("USD");
    ui->cmbHB->addItem("EUR");
    ui->cmbHB->addItem("JPY");
    ui->cmbHB->addItem("GBP");
    ui->cmbHB->addItem("HKD");
    ui->cmbHB->addItem("KRW");
    ui->cmbHB->addItem("AUD");
    ui->cmbHB->addItem("CAD");
    SQL="";
    SQL.append("SELECT nd FROM  nd ");
    SQL.append(" where ");
    SQL.append(" type_id='");
    SQL.append(mySQL::TypeId);
    SQL.append("'  order by nd desc ");
    query.exec(SQL);
    ui->cmbND->clear();
    ui->cmbND->addItem("全部");
    while(query.next()){
        ui->cmbND->addItem(query.value("nd").toString());
    }
    ui->cmbYF->clear();
    ui->cmbYF->addItem("全部");
    for (int i=1;i<12;i++){
        ui->cmbYF->addItem(QString("%1").arg(i,2,10,QChar('0'))); //变量，2位，十进制，替代空字符 如果10 变成2 就变成2进制的了
     }

    ui->treeWidgetXM->clear();
    ui->treeWidgetXM->setHeaderLabels(QStringList() << "项目代码" << "项目简称"  << "项目名称" << "助记码" );
    ui->treeWidgetXM->header()->setSectionResizeMode(QHeaderView::ResizeToContents); //QTreeWidget设置根据内容自适应宽度
    SQL=QString("SELECT xmdm,xmmc,xmjc,xmzj FROM `hsxm` WHERE type_id='%1' and nd='%2'  order by xmdm ").arg(mySQL::TypeId).arg(ND);
    //qDebug() << SQL;
    query.exec(SQL);
    while(query.next()){
        QString str=query.value("xmdm").toString().trimmed();
        QStringList child;
         child.clear();
         child << query.value("xmdm").toString();
         child << query.value("xmjc").toString();
         child << query.value("xmmc").toString();
         child << query.value("xmzj").toString();
         QTreeWidgetItem * citem =new QTreeWidgetItem(child);
         if (str.length() ==1){
             ui->treeWidgetXM->addTopLevelItem(citem);
             ui->treeWidgetXM->expandAll();
         }else{
             //遍历找出父节点
             bool f=false;
             QTreeWidgetItemIterator it(ui->treeWidgetXM);
                 while (*it) {
                      //(*it)->text(0);
                     if ((*it)->text(0)==str.left(str.length()-1)){
                         (*it)->addChild(citem);
                         f=true;
                     }
                     ++it;
                 }
                 if(f==false){
                     //如果减一个字符没照到上级 就减2个字符
                     QTreeWidgetItemIterator it(ui->treeWidgetXM);
                     while (*it) {
                          //(*it)->text(0);
                         if ((*it)->text(0)==str.left(str.length()-2)){
                             (*it)->addChild(citem);
                             f=true;
                         }
                         ++it;
                     }
                 }
                 if(f==false){
                     //如果减2个字符没照到上级 就减3个字符
                     QTreeWidgetItemIterator it(ui->treeWidgetXM);
                     while (*it) {
                          //(*it)->text(0);
                         if ((*it)->text(0)==str.left(str.length()-3)){
                             (*it)->addChild(citem);
                             f=true;
                         }
                         ++it;
                     }
                 }
         }
    }

    QStringList list1;
    list1.clear();
    if (ui->optBWB->isChecked()==true){
        list1<<"凭证日期"<<"年度"<<"凭证编号"<<"会计科目"<<"部门/项目"<<"摘要信息"<<"借贷"<<"金额"<<"余额"<<"外币";
        ui->tableWidget->clear();
        ui->tableWidget->setColumnCount(list1.size());
        ui->tableWidget->setHorizontalHeaderLabels(list1);
        //设置列宽
        ui->tableWidget->setColumnWidth(0, 100);
        ui->tableWidget->setColumnWidth(1, 80);
        ui->tableWidget->setColumnWidth(2, 80);
        ui->tableWidget->setColumnWidth(3, 160);
        ui->tableWidget->setColumnWidth(4, 120);
        ui->tableWidget->setColumnWidth(5, 300);
        ui->tableWidget->setColumnWidth(6, 40);
        ui->tableWidget->setColumnWidth(7, 160);
        ui->tableWidget->setColumnWidth(8, 160);
        ui->tableWidget->setColumnWidth(9, 120);
    }else{
        list1<<"凭证日期"<<"年度"<<"凭证编号"<<"会计科目"<<"部门/项目"<<"摘要信息"<<"借贷"<<"外币"<<"原币金额"<<"原币余额"<<"本位币金额"<<"本位币余额";
        ui->tableWidget->clear();
        ui->tableWidget->setColumnCount(list1.size());
        ui->tableWidget->setHorizontalHeaderLabels(list1);
        //设置列宽
        ui->tableWidget->setColumnWidth(0, 100);
        ui->tableWidget->setColumnWidth(1, 80);
        ui->tableWidget->setColumnWidth(2, 80);
        ui->tableWidget->setColumnWidth(3, 160);
        ui->tableWidget->setColumnWidth(4, 120);
        ui->tableWidget->setColumnWidth(5, 300);
        ui->tableWidget->setColumnWidth(6, 40);
        ui->tableWidget->setColumnWidth(7, 60);
        ui->tableWidget->setColumnWidth(8, 160);
        ui->tableWidget->setColumnWidth(9, 160);
        ui->tableWidget->setColumnWidth(10, 160);
        ui->tableWidget->setColumnWidth(11, 160);
    }


    //ui->tablePZ->horizontalHeader()->setStretchLastSection(true); //设置充满表宽度
    //ui->tableWidget1->verticalHeader()->setResizeMode(QHeaderView::ResizeToContents);
    ui->tableWidget->verticalHeader()->setFixedWidth(48);
    ui->tableWidget->verticalHeader()->setDefaultSectionSize(48); //设置行距
    //ui->tablePZ->horizontalHeader()->resizeSection(0,150); //设置表头第一列的宽度为150
    ui->tableWidget->horizontalHeader()->setFixedHeight(48); //设置表头的高度
    ui->tableWidget->horizontalHeader()->setStyleSheet("QHeaderView::section{background:skyblue;}");
    ui->tableWidget->setStyleSheet("selection-background-color:lightblue;"); //设置选中背景色
    ui->tableWidget->setRowCount(2); //暂时设置成2行
    ui->tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers); //不可编辑
    ui->tableWidget->setSelectionMode(QAbstractItemView::ExtendedSelection);//多选
    ui->tableWidget->horizontalScrollBar()->setEnabled(false);


    ui->tableWidget->setContextMenuPolicy(Qt::CustomContextMenu);       // 右键使能
       menu = new QMenu(ui->tableWidget);
       copySelect = new QAction(menu);
       copySelect->setText("复制选中");
       copyAll = new QAction(menu);
       copyAll->setText("复制全部");
       menu->addAction(copyAll);
       menu->addAction(copySelect);
       //右键位置
       QObject::connect(ui->tableWidget, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(showMenuPos(QPoint)));
       //右键事件
       QObject::connect(menu, SIGNAL(triggered(QAction*)), this, SLOT(MenuSlot(QAction*)));
}

void FormPZCX::MenuSlot(QAction *action) //函数作用:根据行为判断当前所做的事
{

    if(action == copyAll)
    {  //复制全部
        JCode::FZTableWidget(ui->tableWidget);

     }
    if (action == copySelect)
    {//复制选中
        JCode::FZTableWidgetSelect(ui->tableWidget);
    }
}

void FormPZCX::showMenuPos(QPoint)//函数作用:右键菜单显示位置
{
    menu->exec(QCursor::pos());
}



void FormPZCX::RefTree()
{
    ui->treeWidget->clear();
    ui->treeWidget->setHeaderLabels(QStringList() << "科目代码" << "科目名称" << "核算方式" << "核算项目" << "核算货币"<< "余额方向"  );
    ui->treeWidget->header()->setSectionResizeMode(QHeaderView::ResizeToContents); //QTreeWidget设置根据内容自适应宽度
    //ui->treeWidget->setStyleSheet("QTreeWidget::item{border-right: 1px solid blue;border-left: 1px solid blue;border-bottom: 1px solid blue;}"); //加表格线
    QString SQL="";
    QString ND=mySQL::ND;
    QStringList child;
    SQL=QString("SELECT kmdm,kmmc,hsfs,xmbh,hshb,ye FROM `kmxx` WHERE type_id='%1' and nd='%2' order by kmdm").arg(mySQL::TypeId).arg(ND);
    QSqlQuery query(mySQL::cn); //查询 表并输出，测试能否正常操作数据库
    query.exec(SQL);
    while(query.next()){
        QString str=query.value("kmdm").toString().trimmed();
         child.clear();
         child << query.value("kmdm").toString();
         child << query.value("kmmc").toString();
         child << query.value("hsfs").toString();
         child << query.value("xmbh").toString();
         child << query.value("hshb").toString();
         child << query.value("ye").toString();
         QTreeWidgetItem * citem =new QTreeWidgetItem(child);
         if (str.length() ==1){
             ui->treeWidget->addTopLevelItem(citem);
         }else{
             //遍历找出父节点
             bool f=false;
             QTreeWidgetItemIterator it(ui->treeWidget);
                 while (*it) {
                      //(*it)->text(0);
                     if ((*it)->text(0)==str.left(str.length()-1)){
                         (*it)->addChild(citem);
                         f=true;
                     }
                     ++it;
                 }
                 if(f==false){
                     //如果减一个字符没照到上级 就减2个字符
                     QTreeWidgetItemIterator it(ui->treeWidget);
                     while (*it) {
                          //(*it)->text(0);
                         if ((*it)->text(0)==str.left(str.length()-2)){
                             (*it)->addChild(citem);
                             f=true;
                         }
                         ++it;
                     }
                 }
                 if(f==false){
                     //如果减2个字符没照到上级 就减3个字符
                     QTreeWidgetItemIterator it(ui->treeWidget);
                     while (*it) {
                          //(*it)->text(0);
                         if ((*it)->text(0)==str.left(str.length()-3)){
                             (*it)->addChild(citem);
                             f=true;
                         }
                         ++it;
                     }
                 }
         }
    }
}
FormPZCX::~FormPZCX()
{
    delete ui;
}

void FormPZCX::on_cmdUnLoad_clicked()
{
    this->close();
    //delete ui;
}

void FormPZCX::on_treeWidget_itemClicked(QTreeWidgetItem *item, int column)
{
    int i=column;
    i++;
    ui->txtKMDM->setText(item->text(0));

}

void FormPZCX::on_cmdFind_clicked()
{
    QString SQL="";

    QString str="";
    QString SS="";
    QStringList list1;
    list1.clear();
    if (ui->optBWB->isChecked()==true){
        list1<<"凭证日期"<<"年度"<<"凭证编号"<<"会计科目"<<"部门/项目"<<"摘要信息"<<"借方"<<"贷方"<<"余额"<<"外币"<<"外币";
        ui->tableWidget->clear();
        ui->tableWidget->setColumnCount(list1.size());
        ui->tableWidget->setHorizontalHeaderLabels(list1);
        //设置列宽
        ui->tableWidget->setColumnWidth(0, 100);
        ui->tableWidget->setColumnWidth(1, 80);
        ui->tableWidget->setColumnWidth(2, 80);
        ui->tableWidget->setColumnWidth(3, 160);
        ui->tableWidget->setColumnWidth(4, 120);
        ui->tableWidget->setColumnWidth(5, 300);
        ui->tableWidget->setColumnWidth(6, 120);
        ui->tableWidget->setColumnWidth(7, 120);
        ui->tableWidget->setColumnWidth(8, 130);
        ui->tableWidget->setColumnWidth(9, 120);
    }else{
        list1<<"凭证日期"<<"年度"<<"凭证编号"<<"会计科目"<<"部门/项目"<<"摘要信息"<<"借贷"<<"外币"<<"原币金额"<<"原币余额"<<"本位币金额"<<"本位币余额";
        ui->tableWidget->clear();
        ui->tableWidget->setColumnCount(list1.size());
        ui->tableWidget->setHorizontalHeaderLabels(list1);
        //设置列宽
        ui->tableWidget->setColumnWidth(0, 100);
        ui->tableWidget->setColumnWidth(1, 80);
        ui->tableWidget->setColumnWidth(2, 80);
        ui->tableWidget->setColumnWidth(3, 160);
        ui->tableWidget->setColumnWidth(4, 120);
        ui->tableWidget->setColumnWidth(5, 300);
        ui->tableWidget->setColumnWidth(6, 40);
        ui->tableWidget->setColumnWidth(7, 60);
        ui->tableWidget->setColumnWidth(8, 160);
        ui->tableWidget->setColumnWidth(9, 160);
        ui->tableWidget->setColumnWidth(10, 160);
        ui->tableWidget->setColumnWidth(11, 160);
    }


    //ui->tablePZ->horizontalHeader()->setStretchLastSection(true); //设置充满表宽度
    //ui->tableWidget1->verticalHeader()->setResizeMode(QHeaderView::ResizeToContents);
    ui->tableWidget->verticalHeader()->setFixedWidth(48);
    ui->tableWidget->verticalHeader()->setDefaultSectionSize(48); //设置行距
    //ui->tablePZ->horizontalHeader()->resizeSection(0,150); //设置表头第一列的宽度为150
    ui->tableWidget->horizontalHeader()->setFixedHeight(48); //设置表头的高度
    ui->tableWidget->horizontalHeader()->setStyleSheet("QHeaderView::section{background:skyblue;}");
    ui->tableWidget->setStyleSheet("selection-background-color:lightblue;"); //设置选中背景色
    ui->tableWidget->setRowCount(2); //暂时设置成2行
    ui->tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers); //不可编辑
    ui->tableWidget->setSelectionMode(QAbstractItemView::ExtendedSelection);//单选
    ui->tableWidget->horizontalScrollBar()->setEnabled(false);
    str=ui->cmbHB->currentText();
    if (str!="全部"){
        SS += QString(" and hb='%1'").arg(str);
    }
    str=ui->cmbJD->currentText();
    if (str!="全部"){
        SS += QString(" and jd='%1'").arg(str.left(1));
    }

    str=ui->txtSJE->text();
    if (str!=""){
        SS += QString(" and je>=%1").arg(str);
    }
    str=ui->txtEJE->text();
    if (str!=""){
        SS += QString(" and je<=%1").arg(str);
    }
    str=ui->txtSPZH->text();
    if (str!=""){
        SS += QString(" and pzbh>=%1").arg(str);
    }
    str=ui->txtEPZH->text();
    if (str!=""){
        SS += QString(" and pzbh<=%1").arg(str);
    }
    str=ui->txtKMDM->text();
    if (str!=""){
        SS += QString(" and kmdm like '%1%'").arg(str);
    }else{
        QMessageBox::information(this,"提示信息","科目代码为必选项");
        return;
    }
    str=ui->cmbHSXM->currentText();
    if (str!="全部"){
        SS += QString(" and xmbh ='%1'").arg(str.left(str.indexOf(".")));
    }
    str=ui->txtZYXX->text();
    if (str!=""){
        SS += QString(" and zyxx like '%%1%'").arg(str);
    }
    int  Tline=-1; //行号
    double YE=0;
    double YBYE=0;
    //获得期初值： 这里按记账日期来统计
    QTreeWidgetItemIterator it(ui->treeWidget);
    QString tmpKM=ui->txtKMDM->text();
    QString FullName="";
    QString FX="";
         while (*it) {
            if (tmpKM==(*it)->text(0)){
                    FX=(*it)->text(5);
            }
            ++it;
        }
    SQL=QString("SELECT jd,sum(je) as ZJE ,sum(yb) as ZYB FROM `pz` WHERE type_id='%1'  ").arg(mySQL::TypeId);
    SQL+=QString(" and  date_format(rq,'%Y-%m-%d') <'%1' ").arg(ui->dtSRQ->date().toString("yyyy-MM-dd"));
    SQL+=SS;

    SQL+=" group by jd";
    //QMessageBox::information(this,"提示信息",SQL);
    //qDebug()<<SQL;
    QSqlQuery query(mySQL::cn); //查询 表并输出，测试能否正常操作数据库

    query.exec(SQL);
    if (ui->optBWB->isChecked()==true){
        Tline++;
        if (Tline>=ui->tableWidget->rowCount()-1){
            int row = ui->tableWidget->rowCount();
                ui->tableWidget->insertRow(row);
        }
    }

    str="";
    ui->tableWidget->setItem(Tline,6,new QTableWidgetItem(str));
    ui->tableWidget->setItem(Tline,7,new QTableWidgetItem(str));
    while(query.next()){
        if (ui->optBWB->isChecked()==false){
            Tline++;
            if (Tline>=ui->tableWidget->rowCount()-1){
                int row = ui->tableWidget->rowCount();
                    ui->tableWidget->insertRow(row);
            }
        }

        str=QString("上期数【%1前】").arg(ui->dtSRQ->date().toString("yyyy-MM-dd")); // 日期
        //QMessageBox::information(ui->tableWidget,"提示信息",str);
        ui->tableWidget->setItem(Tline,5,new QTableWidgetItem(str));
        str="";

        str=query.value("jd").toString().trimmed(); // 借贷
        //ui->tableWidget->setItem(Tline,6,new QTableWidgetItem(str));
        if (FX=="借方"){
            if (str=="借"){
                YE=YE+query.value("ZJE").toDouble();
                YBYE=YBYE+query.value("ZYB").toDouble();
            }else{
                YE=YE-query.value("ZJE").toDouble();
                YBYE=YBYE-query.value("ZYB").toDouble();
            }
        }else{
            if (str=="借"){
                YE=YE-query.value("ZJE").toDouble();
                YBYE=YBYE-query.value("ZYB").toDouble();

            }else{
                YE=YE+query.value("ZJE").toDouble();
                YBYE=YBYE+query.value("ZYB").toDouble();
            }
        }
        //显示借贷信息
        if (ui->optBWB->isChecked()==true){

            QString JD=query.value("jd").toString().trimmed();
            if (JD=="借"){
                str=QString::number(query.value("ZJE").toDouble(), 'f', 2) ; // 金额
                ui->tableWidget->setItem(Tline,6,new QTableWidgetItem(str));
            }
            if (JD=="贷"){
                str=QString::number(query.value("ZJE").toDouble(), 'f', 2) ; // 金额
                ui->tableWidget->setItem(Tline,7,new QTableWidgetItem(str));
            }

            str=QString::number(YE, 'f', 2) ; // 金额
            ui->tableWidget->setItem(Tline,8,new QTableWidgetItem(str));
            //ui->tableWidget->item(Tline,0)->setTextAlignment(Qt::AlignLeft | Qt::AlignVCenter);
            //ui->tableWidget->item(Tline,1)->setTextAlignment(Qt::AlignLeft | Qt::AlignVCenter);
            //ui->tableWidget->item(Tline,2)->setTextAlignment(Qt::AlignCenter | Qt::AlignVCenter);
            //ui->tableWidget->item(Tline,3)->setTextAlignment(Qt::AlignCenter | Qt::AlignVCenter);
            //ui->tableWidget->item(Tline,4)->setTextAlignment(Qt::AlignLeft | Qt::AlignVCenter);
            ui->tableWidget->item(Tline,5)->setTextAlignment(Qt::AlignLeft | Qt::AlignVCenter);
            ui->tableWidget->item(Tline,6)->setTextAlignment(Qt::AlignRight | Qt::AlignVCenter);
            ui->tableWidget->item(Tline,7)->setTextAlignment(Qt::AlignRight | Qt::AlignVCenter);
            ui->tableWidget->item(Tline,8)->setTextAlignment(Qt::AlignRight | Qt::AlignVCenter);
            //ui->tableWidget->item(Tline,9)->setTextAlignment(Qt::AlignLeft | Qt::AlignVCenter);
        }else{
            str=query.value("jd").toString().trimmed(); // 借贷
            ui->tableWidget->setItem(Tline,6,new QTableWidgetItem(str));
            str=QString::number(query.value("ZYB").toDouble(), 'f', 2) ; // 原币
            ui->tableWidget->setItem(Tline,8,new QTableWidgetItem(str));
            str=QString::number(YBYE, 'f', 2) ; // 原币余额
            ui->tableWidget->setItem(Tline,9,new QTableWidgetItem(str));
            str=QString::number(query.value("ZJE").toDouble(), 'f', 2) ; // 金额
            ui->tableWidget->setItem(Tline,10,new QTableWidgetItem(str));
            str=QString::number(YE, 'f', 2) ; // 金额
            ui->tableWidget->setItem(Tline,11,new QTableWidgetItem(str));
            //ui->tableWidget->item(Tline,0)->setTextAlignment(Qt::AlignLeft | Qt::AlignVCenter);
            //ui->tableWidget->item(Tline,1)->setTextAlignment(Qt::AlignLeft | Qt::AlignVCenter);
            //ui->tableWidget->item(Tline,2)->setTextAlignment(Qt::AlignCenter | Qt::AlignVCenter);
            //ui->tableWidget->item(Tline,3)->setTextAlignment(Qt::AlignCenter | Qt::AlignVCenter);
            //ui->tableWidget->item(Tline,4)->setTextAlignment(Qt::AlignLeft | Qt::AlignVCenter);
            ui->tableWidget->item(Tline,5)->setTextAlignment(Qt::AlignLeft | Qt::AlignVCenter);
            //ui->tableWidget->item(Tline,6)->setTextAlignment(Qt::AlignCenter | Qt::AlignVCenter);
            //ui->tableWidget->item(Tline,7)->setTextAlignment(Qt::AlignCenter | Qt::AlignVCenter);
            ui->tableWidget->item(Tline,8)->setTextAlignment(Qt::AlignRight | Qt::AlignVCenter);
            ui->tableWidget->item(Tline,9)->setTextAlignment(Qt::AlignRight | Qt::AlignVCenter);
            ui->tableWidget->item(Tline,10)->setTextAlignment(Qt::AlignRight | Qt::AlignVCenter);
            ui->tableWidget->item(Tline,11)->setTextAlignment(Qt::AlignRight | Qt::AlignVCenter);

        }

    }


    SS+=QString(" and  date_format(rq,'%Y-%m-%d') >='%1' ").arg(ui->dtSRQ->date().toString("yyyy-MM-dd"));
    SS+=QString(" and  date_format(rq,'%Y-%m-%d') <='%1' ").arg(ui->dtERQ->date().toString("yyyy-MM-dd"));
    str=ui->cmbND->currentText();
    if (str!="全部"){
        SS += QString(" and left(jzny,4)='%1'").arg(str);
    }
    str=ui->cmbYF->currentText();
    if (str!="全部"){
        SS += QString(" and right(jzny,2)='%1'").arg(str);
    }

    if (SS!=""){
        //list1<<"凭证日期"<<"年度"<<"凭证编号"<<"会计科目"<<"部门/项目"<<"摘要信息"<<"借贷"<<"金额"<<"余额"<<"外币";

        SQL=QString("SELECT nd,jzny,pzbh,kmdm,xmbh,jd,zyxx,je,hb,yb,hl,rq,jz FROM `pz` WHERE type_id='%1'  ").arg(mySQL::TypeId);
        SQL+=SS;
        SQL+=" order by nd,jzny,pzbh";
        //QMessageBox::information(this,"提示信息",SQL);
        //qDebug()<<SQL;
        QSqlQuery query(mySQL::cn); //查询 表并输出，测试能否正常操作数据库

        query.exec(SQL);
        while(query.next()){
            Tline++;
            if (Tline>=ui->tableWidget->rowCount()-1){
                int row = ui->tableWidget->rowCount();
                    ui->tableWidget->insertRow(row);
            }
            str=query.value("rq").toString().trimmed(); // 日期
            //QMessageBox::information(ui->tableWidget,"提示信息",str);
            ui->tableWidget->setItem(Tline,0,new QTableWidgetItem(str));
            //QMessageBox::information(this,"提示信息",str);
            str=query.value("jzny").toString().trimmed(); // 记账年月
            ui->tableWidget->setItem(Tline,1,new QTableWidgetItem(str));
            str=query.value("pzbh").toString().trimmed(); // 凭证编号
            ui->tableWidget->setItem(Tline,2,new QTableWidgetItem(str));
            str=query.value("kmdm").toString().trimmed(); //科目代码
            QTreeWidgetItemIterator it(ui->treeWidget);
            QString tmpKM=str;
            QString FullName="";
            QString YEFX="";
                 while (*it) {
                     //(*it)->text(0);
                    if (tmpKM.left((*it)->text(0).length())==(*it)->text(0) && (*it)->text(0).length()>=tmpKM.length()-1){
                            FullName+=QString("\\%1").arg((*it)->text(1));
                    }
                    if (tmpKM==(*it)->text(0)){
                            YEFX=(*it)->text(5);
                    }
                    ++it;
                }
             str+="-"+FullName;
            ui->tableWidget->setItem(Tline,3,new QTableWidgetItem(str));
            str=query.value("xmbh").toString().trimmed(); // 项目编号
            if (str!=""){

                QString tmpXM=str;
                QString XMMC="";
                QStringList sl =str.split(",");
                for (int i=0; i<sl.count(); i++){
                    tmpXM=sl[i];
                    QTreeWidgetItemIterator it(ui->treeWidgetXM);
                    while (*it) {
                       if (tmpXM==(*it)->text(0)){
                               XMMC+=(*it)->text(0);
                               XMMC+=".";
                               XMMC+=(*it)->text(2);
                               XMMC+=",";
                       }
                       ++it;
                   }
                }
                str=XMMC;
            }
            ui->tableWidget->setItem(Tline,4,new QTableWidgetItem(str));
            str=query.value("zyxx").toString().trimmed(); // 摘要信息
            ui->tableWidget->setItem(Tline,5,new QTableWidgetItem(str));
            str=query.value("jd").toString().trimmed(); // 借贷
            ui->tableWidget->setItem(Tline,6,new QTableWidgetItem(str));
            if (YEFX=="借方"){
                if (str=="借"){
                    YE=YE+query.value("je").toDouble();
                    YBYE=YBYE+query.value("yb").toDouble();
                }else{
                    YE=YE-query.value("je").toDouble();
                    YBYE=YBYE-query.value("yb").toDouble();
                }
            }else{
                if (str=="借"){
                    YE=YE-query.value("je").toDouble();
                    YBYE=YBYE-query.value("yb").toDouble();

                }else{
                    YE=YE+query.value("je").toDouble();
                    YBYE=YBYE+query.value("yb").toDouble();
                }
            }
            if (ui->optBWB->isChecked()==true){
                str="";
                ui->tableWidget->setItem(Tline,6,new QTableWidgetItem(str));
                ui->tableWidget->setItem(Tline,7,new QTableWidgetItem(str));
                QString JD=query.value("jd").toString().trimmed();
                if (JD=="借"){
                    str=QString::number(query.value("je").toDouble(), 'f', 2) ; // 借方金额
                    ui->tableWidget->setItem(Tline,6,new QTableWidgetItem(str));
                }
                if (JD=="贷"){
                    str=QString::number(query.value("je").toDouble(), 'f', 2) ; // 贷方金额
                    ui->tableWidget->setItem(Tline,7,new QTableWidgetItem(str));
                }

                str=QString::number(YE, 'f', 2) ; // 金额
                ui->tableWidget->setItem(Tline,8,new QTableWidgetItem(str));
                str=query.value("hb").toString().trimmed();// 货币
                str+=":";
                str+=QString::number(query.value("yb").toDouble(), 'f', 2) ; // 原币 外汇
                str+="(";
                str+=query.value("hl").toString().trimmed();// 汇率
                str+=")";

                if (query.value("hb").toString()=="CNY"){
                    str="";
                }
                ui->tableWidget->setItem(Tline,9,new QTableWidgetItem(str));
                ui->tableWidget->item(Tline,0)->setTextAlignment(Qt::AlignLeft | Qt::AlignVCenter);
                ui->tableWidget->item(Tline,1)->setTextAlignment(Qt::AlignLeft | Qt::AlignVCenter);
                ui->tableWidget->item(Tline,2)->setTextAlignment(Qt::AlignCenter | Qt::AlignVCenter);
                ui->tableWidget->item(Tline,3)->setTextAlignment(Qt::AlignCenter | Qt::AlignVCenter);
                ui->tableWidget->item(Tline,4)->setTextAlignment(Qt::AlignLeft | Qt::AlignVCenter);
                ui->tableWidget->item(Tline,5)->setTextAlignment(Qt::AlignLeft | Qt::AlignVCenter);
                ui->tableWidget->item(Tline,6)->setTextAlignment(Qt::AlignRight | Qt::AlignVCenter);
                ui->tableWidget->item(Tline,7)->setTextAlignment(Qt::AlignRight | Qt::AlignVCenter);
                ui->tableWidget->item(Tline,8)->setTextAlignment(Qt::AlignRight | Qt::AlignVCenter);
                ui->tableWidget->item(Tline,9)->setTextAlignment(Qt::AlignLeft | Qt::AlignVCenter);
            }else{
                str=query.value("hb").toString().trimmed();// 汇率
                ui->tableWidget->setItem(Tline,7,new QTableWidgetItem(str));
                str=QString::number(query.value("yb").toDouble(), 'f', 2) ; // 原币
                ui->tableWidget->setItem(Tline,8,new QTableWidgetItem(str));
                str=QString::number(YBYE, 'f', 2) ; // 原币余额
                ui->tableWidget->setItem(Tline,9,new QTableWidgetItem(str));
                str=QString::number(query.value("je").toDouble(), 'f', 2) ; // 金额
                ui->tableWidget->setItem(Tline,10,new QTableWidgetItem(str));
                str=QString::number(YE, 'f', 2) ; // 金额
                ui->tableWidget->setItem(Tline,11,new QTableWidgetItem(str));
                ui->tableWidget->item(Tline,0)->setTextAlignment(Qt::AlignLeft | Qt::AlignVCenter);
                ui->tableWidget->item(Tline,1)->setTextAlignment(Qt::AlignLeft | Qt::AlignVCenter);
                ui->tableWidget->item(Tline,2)->setTextAlignment(Qt::AlignCenter | Qt::AlignVCenter);
                ui->tableWidget->item(Tline,3)->setTextAlignment(Qt::AlignCenter | Qt::AlignVCenter);
                ui->tableWidget->item(Tline,4)->setTextAlignment(Qt::AlignLeft | Qt::AlignVCenter);
                ui->tableWidget->item(Tline,5)->setTextAlignment(Qt::AlignLeft | Qt::AlignVCenter);
                ui->tableWidget->item(Tline,6)->setTextAlignment(Qt::AlignCenter | Qt::AlignVCenter);
                ui->tableWidget->item(Tline,7)->setTextAlignment(Qt::AlignCenter | Qt::AlignVCenter);
                ui->tableWidget->item(Tline,8)->setTextAlignment(Qt::AlignRight | Qt::AlignVCenter);
                ui->tableWidget->item(Tline,9)->setTextAlignment(Qt::AlignRight | Qt::AlignVCenter);
                ui->tableWidget->item(Tline,10)->setTextAlignment(Qt::AlignRight | Qt::AlignVCenter);
                ui->tableWidget->item(Tline,11)->setTextAlignment(Qt::AlignRight | Qt::AlignVCenter);

            }



        }
    }else{
        QMessageBox::information(this,"提示信息","至少选择一个条件吧！！");
    }

}

void FormPZCX::on_txtKMDM_textChanged(const QString &arg1)
{
    ui->cmbHSXM->clear();
    ui->cmbHSXM->addItem("全部");
    QString SQL="";
    QString ND=mySQL::ND;
    QSqlQuery query(mySQL::cn); //查询 表并输出，测试能否正常操作数据库
    SQL=QString("SELECT xmbh,count(pzid) as t  FROM pz WHERE type_id='%1' and kmdm like '%2%' group by xmbh order by xmbh").arg(mySQL::TypeId).arg(arg1);
    query.exec(SQL);
    //qDebug() << SQL;
    while(query.next()){
        QString tmp=query.value("xmbh").toString().trimmed();
        QTreeWidgetItemIterator it(ui->treeWidgetXM);
        while (*it) {
           if (tmp==(*it)->text(0)){
                   tmp+=".";
                   tmp+=(*it)->text(2);
           }
           ++it;
       }
        ui->cmbHSXM->addItem(tmp);
    }
}

void FormPZCX::on_cmdCopy_clicked()
{

    JCode::FZTableWidget(ui->tableWidget);
}

void FormPZCX::on_cmdPrintPDF_clicked()
{
    QString fileName="";
    fileName= QCoreApplication::applicationDirPath();
    fileName +="/tmp.pdf";
    JCode::TableWidgetTOPDF(ui->tableWidget,fileName,"凭证查询打印",QString("使用单位：%1").arg(mySQL::SYDW),"页尾               ","纵向");
    fileName= QCoreApplication::applicationDirPath();
    fileName +="/tmp1.pdf";
    JCode::TableWidgetTOPDF(ui->tableWidget,fileName,"凭证查询打印",QString("使用单位：%1").arg(mySQL::SYDW),"页尾               ","横向");
}
